package com.edu.exam.entity.grading.correction;

import com.edu.exam.enums.grading.CorrectionType;
import com.edu.exam.enums.grading.CorrectionStatus;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import jakarta.persistence.*;
import jakarta.validation.constraints.DecimalMax;
import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 成绩修正实体类
 *
 * <p>所属模块：E-02 阅卷管理模块</p>
 * <p>对应数据库表：correction</p>
 *
 * <p>记录成绩修正的完整信息，包括修正类型、状态、分数变更、
 * 审批流程等关键信息。支持成绩修正的全生命周期管理。</p>
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@EntityListeners(AuditingEntityListener.class)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class Correction {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull(message = "学生ID不能为空")
    @Column(name = "student_id", nullable = false)
    private Long studentId;

    @NotNull(message = "课程ID不能为空")
    @Column(name = "course_id", nullable = false)
    private Long courseId;

    @NotNull(message = "考核ID不能为空")
    @Column(name = "assessment_id", nullable = false)
    private Long assessmentId;

    @NotNull(message = "修正类型不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "correction_type", nullable = false, length = 20)
    private CorrectionType correctionType;

    @NotNull(message = "修正状态不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "correction_status", nullable = false, length = 20)
    @Builder.Default
    private CorrectionStatus correctionStatus = CorrectionStatus.DRAFT;

    @NotNull(message = "原始分数不能为空")
    @DecimalMin(value = "0.00", message = "原始分数不能小于0")
    @DecimalMax(value = "100.00", message = "原始分数不能大于100")
    @Column(name = "original_score", nullable = false, precision = 5, scale = 2)
    private BigDecimal originalScore;

    @NotNull(message = "修正分数不能为空")
    @DecimalMin(value = "0.00", message = "修正分数不能小于0")
    @DecimalMax(value = "100.00", message = "修正分数不能大于100")
    @Column(name = "corrected_score", nullable = false, precision = 5, scale = 2)
    private BigDecimal correctedScore;

    @NotBlank(message = "修正原因不能为空")
    @Size(max = 1000, message = "修正原因长度不能超过1000个字符")
    @Column(name = "correction_reason", nullable = false, columnDefinition = "TEXT")
    private String correctionReason;

    @NotNull(message = "申请人ID不能为空")
    @Column(name = "applicant_id", nullable = false)
    private Long applicantId;

    @NotNull(message = "申请时间不能为空")
    @CreatedDate
    @Column(name = "application_time", nullable = false, updatable = false)
    private LocalDateTime applicationTime;

    @Column(name = "auditor_id")
    private Long auditorId;

    @Column(name = "audit_time")
    private LocalDateTime auditTime;

    @Column(name = "approver_id")
    private Long approverId;

    @Column(name = "approval_time")
    private LocalDateTime approvalTime;

    @Size(max = 1000, message = "审核意见长度不能超过1000个字符")
    @Column(name = "audit_comment", columnDefinition = "TEXT")
    private String auditComment;

    @Size(max = 1000, message = "审批意见长度不能超过1000个字符")
    @Column(name = "approval_comment", columnDefinition = "TEXT")
    private String approvalComment;

    @Column(name = "deleted", nullable = false)
    @Builder.Default
    private Boolean deleted = false;

    @LastModifiedDate
    @Column(name = "update_time", nullable = false)
    private LocalDateTime updateTime;

    @Column(name = "create_user", nullable = false)
    private Long createUser;

    @Column(name = "update_user", nullable = false)
    private Long updateUser;
}